/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fwdisp.gui.tabs.welcome;

import fwdisp.gui.moduls.SecRowPanel;
import fwdisp.mail.MailSender;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author rbs
 */
public class FeedbackTabPanel extends SecRowPanel {

    JTextField mailField = new JTextField();
    JTextArea textarea = new JTextArea();
    JPanel mailPanel = new JPanel(new BorderLayout());

    public FeedbackTabPanel() {

        JLabel mailLabel = new JLabel("E-Mail: ");
        mailLabel.setPreferredSize(new Dimension(60, 10));

        mailPanel.setBackground(getBackground());
        mailPanel.add(mailLabel, BorderLayout.WEST);
        mailPanel.add(mailField, BorderLayout.CENTER);

        JButton sendButton = new JButton("Absenden");
        sendButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                SendMail();
            }
        });
        setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));
        setLayout(new BorderLayout(40, 40));
        setName("Kontakt");
        mailField.setName("Mail-Adresse");
        add(mailPanel, BorderLayout.NORTH);
        add(textarea, BorderLayout.CENTER);
        add(sendButton, BorderLayout.SOUTH);
    }

    public Boolean isValidMail(String input) {
        String domainsplit[] = input.split("@");
        if (domainsplit.length == 2) {
            if (domainsplit[1].split("\\.").length == 2) {
                return true;
            }
        }

        return false;

    }

    public void SendMail() {
        MailSender sender = new MailSender();
        String receivers[] = {"rbs@informatik.tu-chemnitz.de"};

        try {
            if (isValidMail(mailField.getText())) {
                sender.sendSSLMessage(receivers, "[Feedback FWDisp]", "[Mail: "+ mailField.getText() +"]\r\n" 
                            + "[Nachricht:]\r\n" + textarea.getText(), mailField.getText());
            } else {
                int userInput = JOptionPane.showConfirmDialog(null, "Keine korrekte Mailadresse gefunden.\n\r"
                        + "Wenn Sie die Mailadresse nicht mitteilen kann ich Ihnen nicht antworten.\n\r"
                        + "Möchten Sie ohne eine Mailadresse senden?", "Mailadresse?", JOptionPane.YES_NO_OPTION);

                if (userInput == JOptionPane.YES_OPTION) {
                    sender.sendSSLMessage(receivers, "[Feedback FWDisp]", "[Mail: "+ mailField.getText() +"]" 
                            + "[Nachricht:]\r\n" + textarea.getText(), "fwdisp.info@gmail.com");
                }
            }
        } catch (MessagingException ex) {
            Logger.getLogger(FeedbackTabPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Senden fehlgeschlagen. \n\r"
                    + "Sie können mir auch gern direkt eine Mail senden:\n\r rbs@informatik.tu-chemnitz.de");
        }
    }
}
